package com.jfunit.java.proxy;

import java.lang.reflect.Method;

import org.omg.PortableInterceptor.Interceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfunit.java.util.ClassUtil;

/***********************************************************************   
 *
 * @author        fenfenglee
 * @create-time   2017年8月7日 下午10:43:57
 * @revision      $Id: 1.0  
 *   
 ***********************************************************************/

public abstract class AspectProxy implements Proxy{
    private static final Logger LOGGER = LoggerFactory.getLogger(AspectProxy.class);

    public Object doProxy(ProxyChain proxyChain) throws Throwable {
        Object result = null;
        Class<?> cls = proxyChain.getClass();
        Method method = proxyChain.getTargetMethod();
        Object[] params = proxyChain.getMethodParams();
        
        begin();
        try {
            if (interceptor(cls,method,params)) {
                before(cls,method,params);
                result = proxyChain.doProxyChain();
                after(cls,method,params,result);
            }else{
                result = proxyChain.doProxyChain();
            }
        } catch (Exception e) {
            LOGGER.error("proxy failure",e);
            error(cls,method,params);
            throw e;
        }finally{
            end();
        }
        return result;
    }
    private void begin() {
    }
    
    private boolean interceptor(Class<?> cls, Method method, Object[] params) {
        return true;
    }

    private void before(Class<?> cls, Method method, Object[] params) {
        // TODO Auto-generated method stub
        
    }

    private void after(Class<?> cls, Method method, Object[] params, Object result) {
        // TODO Auto-generated method stub
        
    }

    private void error(Class<?> cls, Method method, Object[] params) {
        // TODO Auto-generated method stub
        
    }

    private void end() {
        // TODO Auto-generated method stub
        
    }

    

    
}
